package Thread;

import org.w3c.dom.css.Counter;

public class Demo9 {
//    使用两个线程来累加 count 的值
//    每个线程循环 1w 次，累加变量 count 的值，count 默认值为 0，注意线程安全问题。

    private static int count = 0;

//    public synchronized void increment() {
//        count++;
//    }

    private static Object locker1 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                synchronized (locker1){
                    count++;
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                synchronized (locker1){
                    count++;
                }
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("final count:" + count);
    }
}



